package com.huaxindata.im.view;

import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.transition.Transition;

/**
 * @author lwjing
 * @date 2018/1/29
 * @Desc 图片 宽高比 或者 高宽比 小于 3 时，保持原宽高比展示
         图片 宽高比 或者 高宽比 大于 3 时，进行截取展示（截取后，长图：高 / 宽 = 3，宽图： 宽 / 高 = 3）
         图片容器（这里是 ImageView）保持跟图片一样的宽高比
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class ChatBitmap extends BitmapImageViewTarget  {
    // 长图，宽图比例阈值
    public static final int RATIO_OF_LARGE = 3;
    // 长图截取后的高宽比（宽图截取后的宽高比）
    public static int HW_RATIO = 3;

    public ChatBitmap(ImageView view)
    {
        super(view);
    }

//
//    @Override
//    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation)
//    {
//        super.onResourceReady(resolveBitmap(resource), glideAnimation);
//    }


    @Override
    public void onResourceReady(Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
        super.onResourceReady(resolveBitmap(resource), transition);
    }

    private Bitmap resolveBitmap(Bitmap resource)
    {
        int srcWidth = resource.getWidth();
        int srcHeight = resource.getHeight();

        if (srcWidth > srcHeight)
        {
            float srcWHRatio = (float) srcWidth / srcHeight;
            // 宽图
            if (srcWHRatio > RATIO_OF_LARGE)
            {
                return Bitmap.createBitmap(resource, 0, 0, srcHeight * HW_RATIO, srcHeight);
            }
        }
        else
        {
            float srcHWRatio = (float) srcHeight / srcWidth;
            // 长图
            if (srcHWRatio > RATIO_OF_LARGE)
            {
                return Bitmap.createBitmap(resource, 0, 0, srcWidth, srcWidth * HW_RATIO);
            }
        }
        return resource;
    }
}
